<?php echo CHtml::beginForm(); ?>
<div class="wide form package">
    <div class="row">
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"marking"); ?>
            <?php echo CHtml::activeTextField($item,"marking",array('maxlength'=>15,'size'=>14)); ?>
        </div>
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"pkg"); ?>
            <?php echo CHtml::activeTextField($item,"pkg",array('maxlength'=>15,'size'=>14)); ?>
        </div>
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"wt"); ?>
            <?php echo CHtml::activeTextField($item,"wt",array('maxlength'=>15,'size'=>14)); ?>
        </div>
    </div>
    <div class="row">
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"width"); ?>
            <?php echo CHtml::activeTextField($item,"width",array('maxlength'=>15,'size'=>14)); ?>
        </div>
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"length"); ?>
            <?php echo CHtml::activeTextField($item,"length",array('maxlength'=>15,'size'=>14)); ?>
        </div>
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"height"); ?>
            <?php echo CHtml::activeTextField($item,"height",array('maxlength'=>15,'size'=>14)); ?>
        </div>
    </div>
    <fieldset id="package_do_list" class="ui-widget section" style="width:85%;margin-left:40px;margin-top:15px;">
    	<legend class="ui-widget-header ui-corner-all">Cargo List</legend>
        <table id="cargo_list">
        	<thead>
        		<tr>
        			<th class="ui-widget-header" width="30%">Ref No.</th>
        			<th class="ui-widget-header" width="20%">Packages</th>
        			<th class="ui-widget-header" width="30%">Weight</th>
        			<th class="ui-widget-header" width="20%">Control</th>
        		</tr>
        	</thead>
        	<tbody>
        		<?php foreach($item->cargo as $g=>$cargo):?>
        		<tr>
        			<td><?php echo CHtml::activeTextField($cargo,"[$g]ref_no",array('cargo_id'=>$cargo->id)); ?></td>
        			<td><?php echo CHtml::activeDropDownList($cargo,"[$g]pkg", $cargo->getAvailableQty()); ?></td>
        			<td><?php echo CHtml::activeTextField($cargo,"[$g]wt",array('size'=>10));echo CHtml::dropDownList("packing_list_packages_cargos[$g][wt_type]",0,cargo_wt::model()->type)?></td>
        			<td><?php echo CHtml::link("<span class='ui-icon ui-icon-extlink'></span>", CHtml::normalizeUrl(array('cargo/update','id'=>$cargo->id)),array('style'=>'display:inline-block;','cargo_ref'=>$cargo->ref_no,'id'=>'icon_link')); ?></td>
        		</tr>
        		<?php endforeach;?>
        	</tbody>
        </table>
    </fieldset>
    <div class="action" id="action">
        <?php echo CHtml::submitButton("Save",array('id'=>'submit','name'=>'submit')); ?>
        <?php echo CHtml::button("Cancel",array('id'=>'Cancel','name'=>'cancel')); ?>
    </div>
</div>
<?php echo CHtml::endForm(); ?>
<?php
$cs = Yii::app()->getClientScript();
$validatecargo = CHtml::normalizeUrl(array('packing_list/ajaxvalidatecargo'));
$script =<<<EOD
$('table#cargo_list input').live('keypress',function(){
    var g=false;
    main = $(this).parents('table#cargo_list>tbody');
    $(main).find("tr:last input").each(function(){
        if($(this).val() !=""){
            g=true;
        }
    });
    if(g) {
        var i = $(main).data('index')+1;
        $(main).data('index',i);
        $(this).parents('tr').clone().appendTo(main);
        $(main).find('tr:last input').val('');
        $(main).find('tr:last input, tr:last select').each(function(){
                var name = $(this).attr("name").match(/\[[\d\w]+\]/gi);
                var oldName='',newName = '';
                for(g=0;g<name.length;g++) {
                    oldName += name[g];
                }
                name[0] = "["+i+"]";
                for(g=0;g<name.length;g++) {
                    newName += name[g];
                }
                $(this).attr('name',$(this).attr('name').replace(oldName,newName));
            });
    }
});
function validNinfo(textfield) {
    var icon = $(textfield).parents('tr').find('a');
    var status = $(textfield).parents('tr').find('span#status');
    var select = $(textfield).parents('tr').find('select:first');
    $(textfield).removeClass('error');
    $(textfield).removeAttr('style');
    $(icon).attr('href','#');
    $(status).html('');
    $(select).html('<option value="0">0</option>');
    if($(textfield).val() == ''){
    	$(textfield).removeAttr('cargo_id');
        return;
    }
    $.ajax({url:'$validatecargo',
        dataType:'json',
        complete:function(){ $(textfield).ajaxloading();},
        success:function(data){
            if(data.r <= 2) {
                $(textfield).addClass('error');
            }
            if(data.r == 0)
                $(icon).attr('href',data.n);
            else
                $(icon).attr('href',data.u);
            var statusText = '';
            if(data.r==0)
                $(status).html('No record is found. Please create new cargo.');
            if(data.r==1||data.r==2)
                statusText +='Error found. ';
            if(data.r==3||data.r==4) {
                statusText += 'No error.';
            }
            if(data.r == 2 || data.r == 3) {
                statusText +='Cargo found is from previous year. Click here to create new cargo.';
            }
            $(status).html(statusText);
            $(textfield).attr('cargo_id',data.id);
            var htm ='';
            for(var i=0;i<=data.qty;i++){
                htm += '<option value='+i+'>'+i+'</option>';
            }
            $(select).html(htm);
            $(select).children('option').last().attr('selected','selected');
        },
        data:{p:'$item->packing_list_id',c:$(textfield).val(),id:'$item->id'},
        beforeSend:function(){ $(textfield).ajaxloading();},
        global:false
        });
}
$('table#cargo_list input').live('change',function(){
    validNinfo($(this));
});
$('table#cargo_list a').live('click',function(){
    var host = $(this);
    var ref_no = $(this).parents('tr').find('input:first').val();
    var cargoform = $.extend({},$.simplelogcontent,{
        afterShow:function(containerID) {
            $(containerID).find("input[type='submit']").unbind();
            $(containerID).find("input[type='submit']").bind({
                'click':  function(){
                    var _cargoform = $.extend({},$.simplelogform,{
                        afterSubmit:function() {
                            validNinfo($(this).parents('tr').find('input:first'));
                            $.simplelogcontent.show($.simplelogcontent.backstepBreadCrumbs());
                        }
                    });
                    _cargoform.Submit($(this).parents('form'),{scene:'packing_list'});
                    return false;
                }
            });
        },
        valuetofield:{
            cargo_ref_no:ref_no
        }
    });
    cargoform.show($(host).attr('href'));
    return false;
});
EOD;
$cs->registerScript("packageScript",$script,CClientScript::POS_END);
?>